package org.jeecg.modules.demo.ddyht.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.ddyht.controller.DdygInventoryController;
import org.jeecg.modules.demo.ddyht.controller.DdygPiciController;
import org.jeecg.modules.demo.ddyht.entity.DdygInventory;
import org.jeecg.modules.demo.ddyht.entity.DdygPici;
import org.jeecg.modules.demo.ddyht.entity.JxcCosting;
import org.jeecg.modules.demo.ddyht.entity.JxcCostingpici;
import org.jeecg.modules.demo.ddyht.mapper.JxcCostingpiciMapper;
import org.jeecg.modules.demo.ddyht.service.IDdygInventoryService;
import org.jeecg.modules.demo.ddyht.service.IDdygPiciService;
import org.jeecg.modules.demo.ddyht.service.IJxcCostingService;
import org.jeecg.modules.demo.ddyht.service.IJxcCostingpiciService;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.Utils.RandomUtil;
import org.jeecg.modules.demo.jxcx.po.JxcCustom;
import org.jeecg.modules.demo.jxcx.po.JxcCzrz;
import org.jeecg.modules.demo.jxcx.po.JxcSale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 成本核算批次信息
 * @Author: jeecg-boot
 * @Date: 2025-05-23
 * @Version: V1.0
 */
@Slf4j
@Service
public class JxcCostingpiciServiceImpl extends ServiceImpl<JxcCostingpiciMapper, JxcCostingpici> implements IJxcCostingpiciService {
    @Autowired
    private IJxcCostingService jxcCostingService;

    @Autowired
    private IDdygInventoryService ddygInventoryService;
    @Autowired
    private IDdygPiciService ddygPiciService;

    @Override
    public Result<String> add(List<JxcCostingpici> jxcCostingpici) {
        log.info("数据新增开始" + jxcCostingpici.toString());
        BigDecimal allamount1 = new BigDecimal("0.00");
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = now.format(formatter);
        String only1 = RandomUtil.generateRandomString();
        String id = now.format(formatter2) + only1;
        String CompanyId = jxcCostingpici.get(0).getCompanyId();
        String Traner = jxcCostingpici.get(0).getTraner().trim();
        List<String> modellist = new ArrayList<>();
        for (int i = 0; i < jxcCostingpici.size(); i++) {
            modellist.add(jxcCostingpici.get(i).getModel());
        }
        for (int i = 0; i < jxcCostingpici.size(); i++) {
            String model = jxcCostingpici.get(i).getModel().trim();
            DdygPici getByModelId = new DdygPici();
            getByModelId =  ddygPiciService.getByModelId(jxcCostingpici.get(i).getOnlyid(),CompanyId, model,  "1");
            if (getByModelId == null) {
                return Result.error("未找到"+ model +"型号的进货信息，不能进行成本核算");
            }
            String number = jxcCostingpici.get(i).getNumber();
            String realnumber = getByModelId.getRealnumber();
            //如果数量比真实数量多，则返回数量不足错误
            if (StringUtils.isNotBlank(realnumber)) {
                if (new BigDecimal(number).compareTo(new BigDecimal(realnumber)) == 1) {
                    return Result.error(model + "型号的批次" + getByModelId.getOnlyid() + "的数量不足，不能进行成本核算");
                }
            }
        }
        for (int i = 0; i < jxcCostingpici.size(); i++) {
            String only = RandomUtil.generateRandomString();
            String model = jxcCostingpici.get(i).getModel().trim();
            String number = jxcCostingpici.get(i).getNumber().trim();
            String unitPrice = jxcCostingpici.get(i).getUnitPrice().trim();
            //用bigdecimal计算总额，小数点位数为0
            String totalfee = String.valueOf(new BigDecimal(number).multiply(new BigDecimal(unitPrice)));
            allamount1 = allamount1.add(new BigDecimal(totalfee));
            jxcCostingpici.get(i).setAmount(totalfee);
//            jxcCostingpici.get(i).setId(id);
            //生成8位随机整数
//            DdygPici getByModelId = new DdygPici();
            DdygPici getByModelId =  ddygPiciService.getByModelId(jxcCostingpici.get(i).getOnlyid(),CompanyId, model,  "1");
            if (StringUtils.isNotBlank(getByModelId.getRealnumber())){
                getByModelId.setRealnumber(""+new BigDecimal(getByModelId.getRealnumber()).subtract(new BigDecimal(number)));
            }else {
                getByModelId.setRealnumber(""+new BigDecimal(getByModelId.getNumber()).subtract(new BigDecimal(number)));
            }
//            jxcCostingpici.get(i).setOnlyid(only + id);
            jxcCostingpici.get(i).setOnlyid(DdygPiciController.setczrzid(getByModelId.getOnlyid()));
            jxcCostingpici.get(i).setId(id);
            log.info("数据保存开始" + jxcCostingpici.toString());
            save(jxcCostingpici.get(i));
            ddygPiciService.updateById(getByModelId);
            if (ddygInventoryService.getBymodelcompany(model, CompanyId) != null) {
                DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(model, CompanyId);
                ddygInventory.setNumber(String.valueOf(new BigDecimal(ddygInventory.getNumber()).subtract(new BigDecimal(number))));
                ddygInventory.setCreateTime(formattedDateTime);
//					BigDecimal multiply = new BigDecimal(unitPrice).multiply(new BigDecimal(number));
//					ddygInventory.setAmount(String.valueOf(new BigDecimal(ddygInventory.getAmount()).subtract(multiply)));
                ddygInventoryService.updateById(ddygInventory);
            } else {
                DdygInventory ddygInventory = new DdygInventory();
//					ddygInventory.setAmount("-" + new BigDecimal(ddygPici.get(i).getAmount()));
                ddygInventory.setNumber("-" + new BigDecimal(jxcCostingpici.get(i).getNumber()));
                ddygInventory.setModel(jxcCostingpici.get(i).getModel());
                ddygInventory.setCompanyId(CompanyId);
                ddygInventory.setCreateTime(formattedDateTime);
                ddygInventoryService.save(ddygInventory);
            }
        }


        //销售
        JxcCosting jxcCosting = new JxcCosting();
        jxcCosting.setAmount(String.valueOf(allamount1));
        jxcCosting.setBuyer(Traner);
        jxcCosting.setCompanyId(CompanyId);
        jxcCosting.setCreateTime(formattedDateTime);
        jxcCosting.setId(id);
        jxcCosting.setModel(modellist.toString());
        jxcCosting.setCreateTime(formattedDateTime);
        jxcCostingService.save(jxcCosting);
        return Result.OK("添加成功！");
    }

    @Override
    public List<JxcCostingpici> getByallId(String companyId, String id) {
        return baseMapper.getByallId(companyId, id);
    }

    @Override
    public void deleteByall(String companyId, String id, String orstate) {
        baseMapper.deleteByall(companyId, id, orstate);
    }

    @Override
    public List<DdygPici> getByall(String companyId, String id, String orstate) {
        return baseMapper.getByall(companyId, id, orstate);
    }

    @Override
    public List<JxcCostingpici> getByModel(String companyId, String model) {
        return baseMapper.getByModel(companyId, model);
    }

    @Override
    public List<JxcCostingpici> getByalltwo(String companyId, String model, String orstate) {
        return baseMapper.getByalltwo(companyId, model, orstate);
    }
}
